﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Security.Policy;
using System.Text;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;

namespace 抖音无水印下载
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //
            //
            if (textBox1.Text == "")
            { MessageBox.Show("请输入要去水印的抖音地址！");return; }
            string url = "";
            string u1 = textBox1.Text;
            if (u1.Contains("复制"))
            {
                int indexu1 = u1.IndexOf("http");
                string u2 = u1.Substring(indexu1);
                //MessageBox.Show(u2);
                int indexu2 = u2.IndexOf("复制");
                string u3 = u2.Substring(0, indexu2);
                //MessageBox.Show(u3);
                 url = u3;
            }
            else
            {  url = textBox1.Text; }
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

                // 设置 AllowAutoRedirect 为 false，防止自动重定向
                request.AllowAutoRedirect = false;

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    if ((int)response.StatusCode >= 301 && (int)response.StatusCode <= 399)
                    {
                        Console.WriteLine("重定向到: " + response.Headers["Location"]);
                        textBox2.Text = response.Headers["Location"];
                    }
                    else
                    {
                        Console.WriteLine("未进行重定向");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("错误信息: " + ex.Message);
            }


            string caijian1 = textBox2.Text;
            string caijian2 = caijian1.Substring(38);
            textBox3.Text = caijian2;


            string caijian3 = textBox3.Text;
            int index = caijian3.IndexOf('/');
            string caijian4 = caijian3.Substring(0, index);
            textBox4.Text = caijian4;

            textBox5.Text = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?reflow_source=reflow_page&item_ids=" + caijian4 + "&a_bogus=64745b2b5bdc4e75b720a9a85b19867a";


            WebClient wc = new WebClient();
            Stream stm = wc.OpenRead(textBox5.Text);
            StreamReader sr = new StreamReader(stm, Encoding.UTF8);
            textBox6.Text= sr.ReadToEnd();

            string caijian5 = textBox6.Text;
            int index1 = caijian5.IndexOf("{\"play_addr\":{\"uri\":\"");
            string caijian6 = caijian5.Substring(index1);
            textBox7.Text = caijian6;

            string caijian7 = textBox7.Text;
            int index2 = caijian7.IndexOf("\",\"url_list\"");
            string caijian8 = caijian7.Substring(0, index2);
            textBox8.Text = caijian8;

            textBox9.Text=caijian8.Remove(0,21);

            textBox10.Text= "www.iesdouyin.com/aweme/v1/play/?video_id="+ textBox9.Text + "&ratio=1080p&line=0";

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox10.Text == "")
            { MessageBox.Show("请先解析地址！");return; }
            try
            {
                Clipboard.SetText(textBox10.Text); // 设置剪贴板上的文本为指定的值

                MessageBox.Show("已成功将文本复制到剪贴板！");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发生错误：{ex.Message}");
            }
        }
    }
}
